package com.kabuda.hot100.二叉树.easy;

import com.kabuda.hot100.二叉树.TreeNode;

/**
 * @author kabuda
 * @date 2025-03-08 17:18
 * @description 给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点。
 */
public class t543_二叉树的直径1 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(9);
        TreeNode treeNode3 = new TreeNode(20);
        TreeNode treeNode4 = new TreeNode(15);
        TreeNode treeNode5 = new TreeNode(7);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode3.left = treeNode4;
        treeNode3.right = treeNode5;

        System.out.println(treeNode1);
        System.out.println(diameterOfBinaryTree(treeNode1));
    }

    static int result;


    public static int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        dfs(root);
        return result;
    }

    public static int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = dfs(root.left);
        int rightDepth = dfs(root.right);
        result = Math.max(result, leftDepth + rightDepth);

        return Math.max(leftDepth, rightDepth) + 1;
    }

}
